package Cm818_Cp_MI;

import CoreClass.*;

/** 
 ��Ǯ ��Ǯ�Ĳ���
*/
public class BankOperate
{
	public BankOperate()
	{
		//
		// TODO: �ڴ˴���ӹ��캯���߼�
		//
	}
	/** 
	 ���˻���Ǯ���� ��Ҫ����Ĳ����б�� �û�ID ��Ǯ����Ŀ, ԭ��,���µ�bank_log
	 
	 @param HyNum
	 @param Money
	 @param Result
	 @return 
	*/
	public final boolean AddMoney(int DgID, int HyNum, java.math.BigDecimal Money, String Remark)
	{
		//�жϴ���� Ǯ ��ʽ�Ƿ���ȷ
		if (Money.compareTo(0) < 0)
		{
			return false;
		}
		//�������
		String sql = "select * from bank where hy_num =" + HyNum;
		CoreClass.DBCommon db = new DBCommon();
		SqlDataReader dr = db.GetDataReaderBySql(sql);
		if (dr == null)
		{
			return false;
		}
		if (!dr.Read())
		{
			return false;
		}
		java.math.BigDecimal money = java.math.BigDecimal.Parse(dr.getItem("balance").toString());
		java.math.BigDecimal Outmoney = java.math.BigDecimal.Parse(dr.getItem("output_money").toString());
		dr.Close();
		//���ڵ��˻����Ӧ��Ϊԭ����������Ľ��
		java.math.BigDecimal nowMoney = Money.add(money); //�������ĳ�ʼ��ֵΪԭ�����������Ľ���
		java.math.BigDecimal nowOutmoney = Money.subtract(Outmoney);
		String sqlUp = "update bank set Balance =" + nowMoney + ",output_money = " + nowOutmoney + " Where hy_num =" + HyNum;
		String sqllog = "insert into  bank_log (balance,bz,addtime,hy_num,typeid,type) values(" + Money + ",'" + Remark + "','" + java.time.LocalDateTime.now().toString() + "'," + HyNum + ",'" + DgID + "','" + "����" + "')";
		try
		{
			db.ExcuteSql(sqlUp);
			db.ExcuteSql(sqllog);
		}
		catch (java.lang.Exception e)
		{
			return false;
		}
		return true;
	}
	/** 
	 ���˻���Ǯ�Ĳ�����Ҫ����Ĳ����б���û�ID ��Ǯ����Ŀ, ԭ��,
	 
	 @param HyNum
	 @param Money
	 @param Result
	 @return 
	*/
	public final boolean minusMoney(int HyNum, java.math.BigDecimal Money, String Remark)
	{
		//�жϽ�Ǯ��ʽ�Ƿ���ȷ
		if (Money.compareTo(0) < 0)
		{
			return false;
		}
		// �������
		String sql = "select * from bank where hy_num =" + HyNum;
		CoreClass.DBCommon db = new DBCommon();
		SqlDataReader dr = db.GetDataReaderBySql(sql);
		if (!dr.Read())
		{
			return false;
		}

		java.math.BigDecimal money = java.math.BigDecimal.Parse(dr.getItem("balance").toString());
		java.math.BigDecimal Outmoney = java.math.BigDecimal.Parse(dr.getItem("output_money").toString());
		dr.Close();
		if (Money.compareTo(money) > 0)
		{
			return false;
		}
		//���ڵ��˻����Ӧ��Ϊԭ�������-����Ľ��
		java.math.BigDecimal nowMoney = Money.subtract(money); //�������ĳ�ʼ��ֵΪԭ�����������Ľ���
		java.math.BigDecimal nowOutmoney = Money.add(Outmoney);
		String sqlUp = "update bank set Balance =" + nowMoney + ",output_money = " + nowOutmoney + " Where hy_num =" + HyNum;
		String sqllog = "insert into  bank_log (balance,bz,addtime,hy_num) values(-" + Money + ",'" + Remark + "','" + java.time.LocalDateTime.now().toString() + "'," + HyNum + ")";
		try
		{
			//ִ�в���
			db.ExcuteSql(sqlUp);
			db.ExcuteSql(sqllog);
		}
		catch (java.lang.Exception e)
		{
			return false;
		}
		return true;
	}
}